/*
 * @Author: dailuyu
 * @Date: 2021-08-17 21:16:07
 * @Email: dly@hyrctec.com
 * @LastEditors: dailuyu
 * @LastEditTime: 2021-08-18 00:18:46
 * @Company: hyrc
 * @FilePath: \TaskErrorManageApi\routes\usersApi.js
 * @Description:
 */
var express = require('express')
var router = express.Router()

let { add, deleted, query, login } = require("../database/Dao/users/users_dao.js") // 数据库操作
let result = require("../util/result.js") // 最终结果统一格式

/**
 * @api {post} /UsersApi/Login
 * @apiDescription 用户登录
 * @apiName Login
 * @apiGroup User
 * @apiParam {string} loginName 用户名
 * @apiParam {string} PassWord 密码
 * @apiSuccess {json} result
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "success" : "true",
 *       "data" : "登录成功"
 *  }
 * @apiSampleRequest http://localhost:3000/UsersApi/Login
 * @apiVersion 1.0.1
 */
router.post('/Login', function (req, res, next) {
  let urlParam = req.body
  console.log(urlParam)
  login(urlParam, function (success) {
    let r = result.createResult(success, "登录成功")
    res.json(r)
  })
})

/**
 * @api {post} /UsersApi/Add 
 * @apiDescription 添加用户数据
 * @apiName Add
 * @apiGroup User
 * @apiParam {json} SendData
 * @apiSuccessExample {json} Success-Request:
 * {
 *   "Id":"",
 *   "UserName":"",
 *   "RoleId":"",
 *   "LoginName":"",
 *   "Password":"",
 *   "Remark":""
 * }
 * @apiSuccess {json} result
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "success" : "true",
 *       "data" : "添加成功"
 *  }
 * @apiSampleRequest http://localhost:3000/UsersApi/Add
 * @apiVersion 1.0.1
 */
router.post('/Add', function (req, res, next) {
  let urlParam = req.body
  console.log(urlParam)
  add(urlParam, function (success) {
    let r = result.createResult(success.affectedRows > 0 ? true : false, success.affectedRows > 0 ? "添加成功" : success.message)
    res.json(r)
  })
})

/**
 * @api {get} /UsersApi/Delete
 * @apiDescription 删除用户
 * @apiName Delete
 * @apiGroup User
 * @apiParam {string} id 用户标识
 * @apiSuccess {json} result
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "success" : "true",
 *      "data" :"删除成功"
 *  }
 * @apiSampleRequest http://localhost:3000/UsersApi/Delete
 * @apiVersion 1.0.1
 */
router.get('/Delete', function (req, res, next) {
  let urlParam = {
    id: req.query.id
  }
  console.log(urlParam)
  deleted(urlParam, function (success) {
    let r = result.createResult(success.affectedRows > 0 ? true : false, success.affectedRows > 0 ? "删除成功" : success.message)
    res.json(r)
  })
})

/**
 * @api {get} /UsersApi/Query
 * @apiDescription 查询用户信息
 * @apiName Query
 * @apiGroup User
 * @apiParam {string} id 用户标识
 * @apiSuccess {json} result
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "success" : "true",
 *      "data" :"结果数据"
 *  }
 * @apiSampleRequest http://localhost:3000/UsersApi/Query
 * @apiVersion 1.0.1
 */
router.get('/Query', function (req, res, next) {
  let urlParam = {
    id: req.query.id
  }
  console.log(urlParam)
  query(urlParam, function (success) {
    let r = result.createResult(true, success)
    res.json(r)
  })
})

module.exports = router
